package no.ntnu.item.ttm4115.termproject.configurationagent.configuration;

import java.io.File;
import java.util.HashMap;
import java.util.Vector;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;


import no.ntnu.item.ttm4115.termproject.extrasagent.Game;
import no.ntnu.item.ttm4115.termproject.extrasagent.Group;
import no.ntnu.item.ttm4115.termproject.extrasagent.SessionOption;
import no.ntnu.item.ttm4115.termproject.extrasagent.SessionQuestion;
import no.ntnu.item.ttm4115.termproject.extrasagent.Treasures;
import no.ntnu.item.ttm4115.termproject.teacheragent.gui.AddTreasure;

import no.tellu.common.actorframe.ActorSM;
import no.tellu.common.javaframe.ActorAddress;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class LoadGameConfiguration {
	private Vector<Treasures> treasures;

	@SuppressWarnings("unused")
	private ActorSM asm;
	private HashMap<String, Game> games;
	
	

	public LoadGameConfiguration(ActorSM asm) {
		super();

		this.asm = asm;		
		// All the treasure configurations will be loaded from the text file and then treasure
		// vector will be populated
		this.treasures = loadTreasuresFromXMLFile();
		this.games = loadAllGamesFromFolder();
		System.out.println(games.size());
	}

	public Vector<Treasures> getTreasures() {
		return treasures;
	}

	public void setTreasures(Vector<Treasures> treasures) {
		this.treasures = treasures;
	}
	public void updateTreasureHash(Treasures treasure,String oldName){
		for (int i = 0; i < treasures.size(); i++) {
			if(treasures.elementAt(i).getName().equals(oldName)){
				String newName = treasure.getName();
				treasures.set(i, treasure);
				treasure.setName(oldName);
				deleteTreasureInXMLFile(treasure.getName());
				treasure.setName(newName);
				writeTreasureToXMLFile(treasure);
				break;
			}
		}
	}
	

	/** 
	* Updating the treasure with actor address when it is initialized
	*/
	public void updateTreasure(ActorAddress actorAddress){
		for(Treasures ta:this.getTreasures()){
			if(ta.getActorAddress() == null){
				int index = this.getTreasures().indexOf(ta);
				ta.setActorAddress(actorAddress);
				this.getTreasures().set(index, ta); // replace the old ta with new one at the same index
				break;
			}
		}
	}
	public void deleteTreasure(Treasures treasure){
		for (int i = 0; i < treasures.size(); i++) {
			if(treasures.elementAt(i).getName().equals(treasure.getName())){
				treasures.remove(i);
				deleteTreasureInXMLFile(treasure.getName());
				break;
			}
		}
	}
		
	@SuppressWarnings("unused")
	public void deleteTreasureInXMLFile(String name){
		try{
			Node treasureToDelete = null;
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse (new File("c:/ttm4115config/configGM.xml"));
			doc.getDocumentElement().normalize();
			NodeList treasures = doc.getElementsByTagName("treasure");
			System.out.println(treasures.getLength());
			for (int i = 0; i < treasures.getLength(); i++) {
				System.out.println(i+" runde");
				Node firstTreasureNode = treasures.item(i); 
				if(firstTreasureNode.getNodeType() == Node.ELEMENT_NODE){
					Element firstTreasureElement = (Element)firstTreasureNode;
					
					NodeList treasureNameList = firstTreasureElement.getElementsByTagName("name");
					Element treasureNameElement = (Element)treasureNameList.item(0);
					
					NodeList textList = treasureNameElement.getChildNodes();
					String nameText = ((Node)textList.item(0)).getNodeValue().trim();
					System.out.println("!!!!!!!!!!!!!!!");
					System.out.println(nameText);
					System.out.println(name);
					if(nameText.equals(name)){
						System.out.println("Her er den inne i if");
						treasureToDelete = firstTreasureNode;
						break;
					}
					
				}	
			}
			if(treasureToDelete != null){
				
				Node parent = treasureToDelete.getParentNode();
				parent.removeChild(treasureToDelete);
				
				Transformer tFormer = TransformerFactory.newInstance().newTransformer();

		        tFormer.setOutputProperty(OutputKeys.METHOD, "xml");
		        tFormer.setOutputProperty(OutputKeys.INDENT,"yes" );
			    tFormer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

		        Source source = new DOMSource(doc);
		        Result result = new StreamResult(new File("c:/ttm4115config/configGM.xml"));
		        tFormer.transform(source, result);
		   	}
			
		}catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
			System.out.println(" " + err.getMessage ());

		}catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();

		}catch (Throwable t) {
			t.printStackTrace ();
		}
	}
	
	public void writeTreasureToXMLFile(Treasures treasures2) {
		try{
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse (new File("c:/ttm4115config/configGM.xml"));
			
			doc.getDocumentElement().normalize();
			NodeList treasures = doc.getElementsByTagName("treasures");
			
			Node newTreasure = doc.createElement("treasure");
			newTreasure.setTextContent("");
			treasures.item(0).appendChild(newTreasure);
			
			Node treasureName = doc.createElement("name");
			treasureName.setTextContent(treasures2.getName());
			newTreasure.appendChild(treasureName);
			
			for (int i = 0; i < treasures2.getClue().size(); i++) {
				Node treasureClue = doc.createElement("clue");
				treasureClue.setTextContent(treasures2.getClue().elementAt(i));
				newTreasure.appendChild(treasureClue);
			}
			Node xCoord = doc.createElement("xcoordinate");
			xCoord.setTextContent(""+treasures2.getXCoordinate());
			newTreasure.appendChild(xCoord);
			
			Node yCoord = doc.createElement("ycoordinate");
			yCoord.setTextContent(""+treasures2.getYCoordinate());
			newTreasure.appendChild(yCoord);
			
			for (int i = 0; i < treasures2.getQuestions().size(); i++) {
				Node treasureQuestion = doc.createElement("question");
				treasureQuestion.setTextContent("");
				
				for (int k = 0; k < treasures2.getQuestions().elementAt(i).getQuestionText().size(); k++) {
					Node treasureText = doc.createElement("text");
					treasureText.setTextContent(treasures2.getQuestions().elementAt(i).getQuestionText().elementAt(k));
					treasureQuestion.appendChild(treasureText);
				}
				Node questionType = doc.createElement("type");
				questionType.setTextContent("MultipleChoiceQuestionAgent");
				treasureQuestion.appendChild(questionType);
				for (int j = 0; j < treasures2.getQuestions().elementAt(i).getOptions().size(); j++) {
					Node questionOption = doc.createElement("option");
					questionOption.setTextContent(treasures2.getQuestions().elementAt(i).getOptions().elementAt(j).getAnswerOption());
					treasureQuestion.appendChild(questionOption);
				}
				Node correctAnswer = doc.createElement("correctanswerindex");
				correctAnswer.setTextContent(""+treasures2.getQuestions().elementAt(i).getCorrectAnswerIndex());
				treasureQuestion.appendChild(correctAnswer);
				newTreasure.appendChild(treasureQuestion);
			}
						
			Transformer tFormer = TransformerFactory.newInstance().newTransformer();

	        tFormer.setOutputProperty(OutputKeys.METHOD, "xml");
	        tFormer.setOutputProperty(OutputKeys.INDENT,"yes" );
		    tFormer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

	        Source source = new DOMSource(doc);
	        Result result = new StreamResult(new File("c:/ttm4115config/configGM.xml"));
	        tFormer.transform(source, result);
	        System.out.println("File creation successfully!");
			
			
		}catch (Throwable t) {
			t.printStackTrace ();
		}
		
	}
	
	
	
	
	
	public void writeGameToXMLFile(Game g){
		try{
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.newDocument();
			
			Node gameNode = doc.createElement("game");
			
			Node nameNode = doc.createElement("name");
			nameNode.setTextContent(g.getName());
			
			gameNode.appendChild(nameNode);
			
			Node groupsNode = doc.createElement("groups");
			
			gameNode.appendChild(groupsNode);
			
			for (int i = 0; i < g.getGroups().size(); i++) {
				Node newGroupNode = doc.createElement("group");
				newGroupNode.setTextContent(g.getGroups().elementAt(i).getGroupName());
				groupsNode.appendChild(newGroupNode);
			}
			Node treasuresNode = doc.createElement("treasures");
			
			
			
			for (int i = 0; i < g.getTreasures().size(); i++) {
				Node newTreasure = doc.createElement("treasure");
				
				Node newTreasureName = doc.createElement("treasurename");
				newTreasureName.setTextContent(g.getTreasures().elementAt(i).getName());
				//newTreasure.setTextContent(g.getTreasures().elementAt(i).getName());
				newTreasure.appendChild(newTreasureName);
				
				for (int q = 0; q < g.getTreasures().elementAt(i).getClue().size(); q++) {
					Node newTreasureClue = doc.createElement("clue");
					newTreasureClue.setTextContent(g.getTreasures().elementAt(i).getClue().elementAt(q));
					newTreasure.appendChild(newTreasureClue);
				}
				
				Node xNode = doc.createElement("xcoordinate");
				xNode.setTextContent(""+g.getTreasures().elementAt(i).getXCoordinate());
				newTreasure.appendChild(xNode);
				
				Node yNode = doc.createElement("ycoordinate");
				yNode.setTextContent(""+g.getTreasures().elementAt(i).getYCoordinate());
				newTreasure.appendChild(yNode);
				for (int j = 0; j < g.getTreasures().elementAt(i).getQuestions().size(); j++) {
					Node questionsNode = doc.createElement("question");	
					for (int m = 0; m < g.getTreasures().elementAt(i).getQuestions().elementAt(j).getQuestionText().size(); m++) {
		
					
					Node textNode = doc.createElement("text");
					textNode.setTextContent(g.getTreasures().elementAt(i).getQuestions().elementAt(j).getQuestionText().elementAt(m));
					questionsNode.appendChild(textNode);
					}
					Node typeNode = doc.createElement("type");
					typeNode.setTextContent(g.getTreasures().elementAt(i).getQuestions().elementAt(j).getType());
					questionsNode.appendChild(typeNode);
					for (int k = 0; k < g.getTreasures().elementAt(i).getQuestions().elementAt(j).getOptions().size(); k++) {
						Node optionNode = doc.createElement("option");
						optionNode.setTextContent(g.getTreasures().elementAt(i).getQuestions().elementAt(j).getOptions().elementAt(k).getAnswerOption());
						questionsNode.appendChild(optionNode);
					}
										
					Node correctNode = doc.createElement("correctanswerindex");
					correctNode.setTextContent(""+g.getTreasures().elementAt(i).getQuestions().elementAt(j).getCorrectAnswerIndex());
					questionsNode.appendChild(correctNode);
					
					newTreasure.appendChild(questionsNode);
				}
				treasuresNode.appendChild(newTreasure);
				
				gameNode.appendChild(treasuresNode);
				
			}
			doc.appendChild(gameNode);
			doc.normalizeDocument();
			
			Transformer tFormer = TransformerFactory.newInstance().newTransformer();

	        tFormer.setOutputProperty(OutputKeys.METHOD, "xml");
	        tFormer.setOutputProperty(OutputKeys.INDENT,"yes" );
	        tFormer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

	        Source source = new DOMSource(doc);
	        Result result = new StreamResult(new File("c:/ttm4115config/games/"+g.getName()+".xml"));
	        tFormer.transform(source, result);
	        System.out.println("File creation successfully!");
			
			
			
		}catch (Throwable t) {
			t.printStackTrace ();
		}
	}
	private Vector<Treasures> loadTreasuresFromXMLFile() {
		treasures = new Vector<Treasures>();
		try {
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse (new File("c:/ttm4115config/configGM.xml"));

			// normalize text representation
			doc.getDocumentElement ().normalize ();
			System.out.println ("Root element of the doc is " + doc.getDocumentElement().getNodeName());


			NodeList listOfTreasures = doc.getElementsByTagName("treasure");
			int totalPersons = listOfTreasures.getLength();
			System.out.println("Total no of treasures : " + totalPersons);

			for(int s=0; s<listOfTreasures.getLength() ; s++){

				Node firstPersonNode = listOfTreasures.item(s);
				if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){

					Element firstPersonElement = (Element)firstPersonNode;

					//-------
					NodeList treasureNameList = firstPersonElement.getElementsByTagName("name");
					Element treasureNameElement = (Element)treasureNameList.item(0);

					NodeList textTNList = treasureNameElement.getChildNodes();
					String treasureName = ((Node)textTNList.item(0)).getNodeValue().trim();
					System.out.println("Loading Treasure Name : " + treasureName);


					//-------
					NodeList treasureClueList = firstPersonElement.getElementsByTagName("clue");
					Vector<String> clues = new Vector<String>();
					
					for (int o = 0; o < treasureClueList.getLength(); o++) {
						Element treasureClueElement = (Element)treasureClueList.item(o);
						NodeList textClueList = treasureClueElement.getChildNodes();
						System.out.println(textClueList.getLength());
						String treasureClue = ((Node)textClueList.item(0)).getNodeValue().trim();
						System.out.println("Loading Clue : " + treasureClue);
						clues.add(treasureClue);
					}
					

					//----
					NodeList xcorList = firstPersonElement.getElementsByTagName("xcoordinate");
					Element xcorElement = (Element)xcorList.item(0);

					NodeList textXcList = xcorElement.getChildNodes();
					int xCor = Integer.valueOf(((Node)textXcList.item(0)).getNodeValue().trim());
					System.out.println("Loading X-Coordinate : " + xCor);

					//------
					NodeList ycorList = firstPersonElement.getElementsByTagName("ycoordinate");
					Element ycorElement = (Element)ycorList.item(0);

					NodeList textYcList = ycorElement.getChildNodes();
					int yCor = Integer.valueOf(((Node)textYcList.item(0)).getNodeValue().trim());
					System.out.println("Loading Y-Coordinate : " + yCor);

					//------ Loading the questions

					NodeList listOfQuestions = firstPersonElement.getElementsByTagName("question");
					int totalQuestions = listOfQuestions.getLength();
					System.out.println("Total no of Questions : " + totalQuestions);

					Vector<SessionQuestion> questions = new Vector<SessionQuestion>();
					for(int p=0; p<listOfQuestions.getLength() ; p++){
						
						SessionQuestion question = new SessionQuestion();
						Node firstQuestionNode = listOfQuestions.item(p);
						if(firstQuestionNode.getNodeType() == Node.ELEMENT_NODE){

							Element firstQuestionElement = (Element)firstQuestionNode;
												

							//------- question text
							NodeList questionTextList = firstQuestionElement.getElementsByTagName("text");
							Vector<String> questionText = new Vector<String>();
							for (int t = 0; t < questionTextList.getLength(); t++) {
								Element questionTextElement = (Element)questionTextList.item(t);
								
								NodeList textList = questionTextElement.getChildNodes();
								String questionTextString = ((Node)textList.item(0)).getNodeValue().trim();
								System.out.println("Loading QuestionText : " + questionTextString);
								questionText.add(questionTextString);								
							}
								question.setQuestionText(questionText);
							
							question.setId(p);
							
							
							
							NodeList typeTextList = firstQuestionElement.getElementsByTagName("type");
							Element typeTextElement = (Element)typeTextList.item(0);
							

							NodeList typeList = typeTextElement.getChildNodes();
							String typeText = ((Node)typeList.item(0)).getNodeValue().trim();
																										
							question.setType(typeText);
							System.out.println("Lastet inn question med type " + typeText);
							
							Vector<SessionOption> options = new Vector<SessionOption>();
							//----- options
							NodeList questionOptionList = firstQuestionElement.getElementsByTagName("option");							
							for(int q=0; q<questionOptionList.getLength() ; q++){
								
								SessionOption option = new SessionOption();
								
								Element questionOptionElement = (Element)questionOptionList.item(q);
								NodeList optionList = questionOptionElement.getChildNodes();

								String optionText = ((Node)optionList.item(0)).getNodeValue().trim();
								System.out.println("Loading option "+q+" : " + optionText);
								
								option.setAnswerOption(optionText);
								options.add(option);
							}
							question.setOptions(options);

							//------- check correctness
							NodeList correctAnswerList = firstQuestionElement.getElementsByTagName("correctanswerindex");
							Element correctAnswerElement = (Element)correctAnswerList.item(0);

							NodeList answerList = correctAnswerElement.getChildNodes();
							int correctAnswerIndex = Integer.valueOf(((Node)answerList.item(0)).getNodeValue().trim());
							System.out.println("Loading correctanswerindex : " + correctAnswerIndex);
							
							question.setCorrectAnswerIndex(correctAnswerIndex);
						}
						
						questions.add(question);
					}

					// Populate the treasure object and add to vector
					treasures.add(new Treasures(null, clues, treasureName, xCor, yCor, questions));
					
				}//end of if clause

				
			}//end of for loop with s var


		}catch (SAXParseException err) {
			System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
			System.out.println(" " + err.getMessage ());

		}catch (SAXException e) {
			Exception x = e.getException ();
			((x == null) ? e : x).printStackTrace ();

		}catch (Throwable t) {
			t.printStackTrace ();
		}
		//System.exit (0);

		return treasures;
	}
	public HashMap<String, Game> loadAllGamesFromFolder(){
		String path = "c:/ttm4115config/games/";
		File file = new File(path);
		String[] list = file.list();
		games = new HashMap<String, Game>();
		for (int i = 0; i < list.length; i++) {
			
			Game newGame = new Game();
			Vector<Group> groupVector = new Vector<Group>();
			try{
				DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
				DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
				Document doc = docBuilder.parse (new File(path+list[i]));
				
				doc.getDocumentElement ().normalize ();
				//--- Load game name
				Node gameName = doc.getElementsByTagName("name").item(0);
				if(gameName.getNodeType() == Node.ELEMENT_NODE) {
					Element nameElement = (Element)gameName;
					NodeList nameList = nameElement.getChildNodes();
					String name = ((Node)nameList.item(0)).getNodeValue().trim();
					newGame.setName(name);
					System.out.println("Loading game: "+name);
				}
				
				NodeList groups = doc.getElementsByTagName("group");
				for (int j = 0; j < groups.getLength(); j++) {
					Group group = new Group();
					Element groupElement = (Element)groups.item(j);
					NodeList groupList = groupElement.getChildNodes();

					String groupName = ((Node)groupList.item(0)).getNodeValue().trim();
					
					group.setGroupName(groupName);
					groupVector.add(group);
				}
				newGame.setGroups(groupVector);
				Vector<Treasures> gameTreasures = new Vector<Treasures>();
				NodeList listOfTreasures = doc.getElementsByTagName("treasure");
				

				for(int s=0; s<listOfTreasures.getLength() ; s++){

					Node firstPersonNode = listOfTreasures.item(s);
					if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){

						Element firstPersonElement = (Element)firstPersonNode;

						//-------
						NodeList treasureNameList = firstPersonElement.getElementsByTagName("treasurename");
						Element treasureNameElement = (Element)treasureNameList.item(0);

						NodeList textTNList = treasureNameElement.getChildNodes();
						String treasureName = ((Node)textTNList.item(0)).getNodeValue().trim();
						
						//-------
						NodeList treasureClueList = firstPersonElement.getElementsByTagName("clue");
						Vector<String> clues = new Vector<String>();
						
						for (int o = 0; o < treasureClueList.getLength(); o++) {
							Element treasureClueElement = (Element)treasureClueList.item(o);
							NodeList textClueList = treasureClueElement.getChildNodes();
							
							String treasureClue = ((Node)textClueList.item(0)).getNodeValue().trim();
							
							clues.add(treasureClue);
						}
						

						//----
						NodeList xcorList = firstPersonElement.getElementsByTagName("xcoordinate");
						Element xcorElement = (Element)xcorList.item(0);

						NodeList textXcList = xcorElement.getChildNodes();
						int xCor = Integer.valueOf(((Node)textXcList.item(0)).getNodeValue().trim());
						

						//------
						NodeList ycorList = firstPersonElement.getElementsByTagName("ycoordinate");
						Element ycorElement = (Element)ycorList.item(0);

						NodeList textYcList = ycorElement.getChildNodes();
						int yCor = Integer.valueOf(((Node)textYcList.item(0)).getNodeValue().trim());
						

						//------ Loading the questions

						NodeList listOfQuestions = firstPersonElement.getElementsByTagName("question");
						int totalQuestions = listOfQuestions.getLength();
						

						Vector<SessionQuestion> questions = new Vector<SessionQuestion>();
						for(int p=0; p<listOfQuestions.getLength() ; p++){
							
							SessionQuestion question = new SessionQuestion();
							Node firstQuestionNode = listOfQuestions.item(p);
							if(firstQuestionNode.getNodeType() == Node.ELEMENT_NODE){

								Element firstQuestionElement = (Element)firstQuestionNode;
													

								//------- question text
								NodeList questionTextList = firstQuestionElement.getElementsByTagName("text");
								Vector<String> questionText = new Vector<String>();
								for (int t = 0; t < questionTextList.getLength(); t++) {
									Element questionTextElement = (Element)questionTextList.item(t);
									
									NodeList textList = questionTextElement.getChildNodes();
									String questionTextString = ((Node)textList.item(0)).getNodeValue().trim();
									questionText.add(questionTextString);								
								}
									question.setQuestionText(questionText);
								
								question.setId(p);
								
								
								
								NodeList typeTextList = firstQuestionElement.getElementsByTagName("type");
								Element typeTextElement = (Element)typeTextList.item(0);
								

								NodeList typeList = typeTextElement.getChildNodes();
								String typeText = ((Node)typeList.item(0)).getNodeValue().trim();
																											
								question.setType(typeText);
								
								
								Vector<SessionOption> options = new Vector<SessionOption>();
								//----- options
								NodeList questionOptionList = firstQuestionElement.getElementsByTagName("option");							
								for(int q=0; q<questionOptionList.getLength() ; q++){
									
									SessionOption option = new SessionOption();
									
									Element questionOptionElement = (Element)questionOptionList.item(q);
									NodeList optionList = questionOptionElement.getChildNodes();

									String optionText = ((Node)optionList.item(0)).getNodeValue().trim();
																		
									option.setAnswerOption(optionText);
									options.add(option);
								}
								question.setOptions(options);

								//------- check correctness
								NodeList correctAnswerList = firstQuestionElement.getElementsByTagName("correctanswerindex");
								Element correctAnswerElement = (Element)correctAnswerList.item(0);

								NodeList answerList = correctAnswerElement.getChildNodes();
								int correctAnswerIndex = Integer.valueOf(((Node)answerList.item(0)).getNodeValue().trim());
															
								question.setCorrectAnswerIndex(correctAnswerIndex);
							}
							
							questions.add(question);
						}

						// Populate the treasure object and add to vector
						gameTreasures.add(new Treasures(null, clues, treasureName, xCor, yCor, questions));
						
					}
					newGame.setTreasures(gameTreasures);
				}
				games.put(newGame.getName(), newGame);
				
			}catch (SAXParseException err) {
				System.out.println ("** Parsing error" + ", line " + err.getLineNumber () + ", uri " + err.getSystemId ());
				System.out.println(" " + err.getMessage ());

			}catch (SAXException e) {
				Exception x = e.getException ();
				((x == null) ? e : x).printStackTrace ();

			}catch (Throwable t) {
				t.printStackTrace ();
			}
		}
		return games;
		
	}

	public ActorSM getAsm() {
		return asm;
	}

	public void setAsm(ActorSM asm) {
		this.asm = asm;
	}

	public HashMap<String, Game> getGames() {
		return games;
	}

	public void setGames(HashMap<String, Game> games) {
		this.games = games;
	}

	
}
